package com.cms.gdm.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cms.common.utils.R;
import com.cms.gdm.dao.GuideDao;
import com.cms.gdm.dao.TeacherDao;
import com.cms.gdm.entity.GuideEntity;
import com.cms.gdm.entity.TeacherEntity;
import com.cms.gdm.entity.vo.ProjectClientVo;
import com.cms.gdm.entity.vo.ProjectVo;
import com.cms.gdm.feign.StuFeign;
import com.cms.gdm.to.StudentTo;
import org.bouncycastle.util.Arrays;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.common.utils.PageUtils;
import com.cms.common.utils.Query;

import com.cms.gdm.dao.ProjectDao;
import com.cms.gdm.entity.ProjectEntity;
import com.cms.gdm.service.ProjectService;

import javax.annotation.Resource;


@Service("projectService")
public class ProjectServiceImpl extends ServiceImpl<ProjectDao, ProjectEntity> implements ProjectService {

    @Resource
    private GuideDao guideDao;

    @Autowired
    private StuFeign stuFeign;

    @Resource
    private TeacherDao teacherDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ProjectEntity> wrapper = new QueryWrapper<>();
        wrapper.select("guide_id, GROUP_CONCAT(address) as addresses, group_concat(version) as versions, group_concat(create_time) as times");
        wrapper.groupBy("guide_id");
        IPage<ProjectEntity> page = this.page(
                new Query<ProjectEntity>().getPage(params),
                wrapper
        );

        List<ProjectVo> collect = page.getRecords().stream().map(item -> {
            ProjectVo projectVo = new ProjectVo();
            BeanUtils.copyProperties(item, projectVo);
            GuideEntity guideEntity = guideDao.selectById(item.getGuideId());
            R r = stuFeign.info(guideEntity.getStudentId());
            if (r.getCode() == 0) {
                StudentTo student = r.getData("student", new TypeReference<StudentTo>() {
                });
                projectVo.setStudentName(student.getName());
            }

            TeacherEntity teacherEntity = teacherDao.selectById(guideEntity.getTeacherId());
            projectVo.setTeacherName(teacherEntity.getName());
            return projectVo;
        }).collect(Collectors.toList());

        IPage<ProjectVo> projectVoIPage = new Page<>();
        projectVoIPage.setTotal(page.getTotal());
        projectVoIPage.setSize(page.getSize());
        projectVoIPage.setPages(page.getPages());
        projectVoIPage.setCurrent(page.getCurrent());
        projectVoIPage.setRecords(collect);

        return new PageUtils(projectVoIPage);
    }

    @Override
    public ProjectClientVo selectProjectByStudentId(Long studentId) {
        ProjectClientVo projectClientVo = new ProjectClientVo();

        GuideEntity guideEntity = guideDao.selectOne(new QueryWrapper<GuideEntity>().eq("student_id", studentId));
        QueryWrapper<ProjectEntity> wrapper = new QueryWrapper<>();
        wrapper.select("guide_id, GROUP_CONCAT(address) as addresses, group_concat(version) as versions, group_concat(create_time) as times, group_concat(r_address) as rAddresses, group_concat(update_time) as uTimes");
        wrapper.groupBy("guide_id");
        wrapper.eq("guide_id", guideEntity.getId());
        ProjectEntity projectEntity = baseMapper.selectOne(wrapper);
        BeanUtils.copyProperties(projectEntity, projectClientVo);

        Long teacherId = guideEntity.getTeacherId();
        TeacherEntity teacherEntity = teacherDao.selectById(teacherId);
        projectClientVo.setTeacherName(teacherEntity.getName());
        projectClientVo.setProjectName(teacherEntity.getSubject());

        R r = stuFeign.info(studentId);
        if(r.getCode() == 0){
            StudentTo student = r.getData("student", new TypeReference<StudentTo>() {
            });
            projectClientVo.setStudentGender(student.getGender());
            projectClientVo.setStudentName(student.getName());
        }

        return projectClientVo;
    }

    @Override
    public ProjectEntity selectProjectByGuide(Long guideId) {
        QueryWrapper<ProjectEntity> wrapper = new QueryWrapper<>();
        wrapper.select("guide_id, GROUP_CONCAT(address) as addresses, group_concat(version) as versions, group_concat(create_time) as times, group_concat(r_address) as rAddresses, group_concat(update_time) as uTimes");
        wrapper.groupBy("guide_id");
        wrapper.eq("guide_id", guideId);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public void updateProjectByTeacher(ProjectEntity project) {
        List<ProjectEntity> projectEntities = baseMapper.selectBaseList(project.getGuideId());
        Collections.reverse(projectEntities);
        ProjectEntity projectEntity = projectEntities.get(0);
        if(projectEntity.getRAddress() == null ||projectEntity.getRAddress().equals("")){
            projectEntity.setRAddress(project.getRAddress());
        }
        baseMapper.updateById(projectEntity);
    }

}